import logging
from io import BytesIO
from urllib import parse
from qiniu import Auth, put_data

# 需要填写你的 Access Key 和 Secret Key
access_key = 'otARiQ1onKFh7MhntzVkXRlMRX5lTj_s1p-aY-3m'
secret_key = 'YYvDL8p9LPO8QTGDKmz0RbvZouSd16nDcBcrhXdN'
bucket_url = 'http://qcz5y1tis.bkt.clouddn.com/'


# 要上传的空间
bucket_name = 'ihomelxy'


def _upload_to_qiniu(upfile):
    """
    上传文件到七牛
    """
    try:
        q = Auth(access_key, secret_key)
        token = q.upload_token(bucket_name)
        ret, info = put_data(token, None, upfile)
    except Exception as e:
        logging.error(e)
        raise e
    if info.ok:
        # 配置自己的域名：DOMAIN:http://七牛云的域名/
        url = parse.urljoin(bucket_url, ret['key'])
        return url, ret['key']
    else:

        return 'FAIL', None, None, None

# def storage(data):
#     """七牛云存储上传文件接口"""
#     if not data:
#         return None
#     try:
#         # 构建鉴权对象
#         q = Auth(access_key, secret_key)
#
#         # 生成上传 Token，可以指定过期时间等
#         token = q.upload_token(bucket_name)
#
#         # 上传文件
#         ret, info = put_data(token, None, data)
#
#     except Exception as e:
#         logging.error(e)
#         raise e
#
#     if info and info.status_code != 200:
#         raise Exception("上传文件到七牛失败")
#
#     # 返回七牛中保存的图片名，这个图片名也是访问七牛获取图片的路径
#     print(ret["key"])
#     return ret["key"]
#
#
# if __name__ == '__main__':
#     file_name = input("输入上传的文件")
#     with open(file_name, "rb") as f:
#         storage(f.read())


